package com.blockchain.console.servlets.trading;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.common.enums.EntrustType;
import com.blockchain.common.enums.JyztStatus;
import com.blockchain.console.service.trading.TradingManage;
import com.blockchain.console.service.trading.entity.JyglEntity;
import com.blockchain.console.service.trading.query.TradingQuery;
import com.blockchain.console.servlets.financial.AbstractFinancialServlet;
import com.blockchain.framework.http.servlet.annotation.Right;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.framework.service.query.Paging;
import com.blockchain.framework.service.query.PagingResult;
import com.blockchain.util.parser.EnumParser;
import com.blockchain.util.parser.IntegerParser;
import com.blockchain.util.parser.TimestampParser;

@Right(id = "C_TRADING_EXPORWT", name = "交易管理-委托管理-导出")
public class ExportWt extends AbstractFinancialServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void processGet(HttpServletRequest request,
			HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		processPost(request, response, serviceSession);
	}

	@Override
	protected void processPost(final HttpServletRequest request,
			final HttpServletResponse response,
			final ServiceSession serviceSession) throws Throwable {
		response.setHeader("Content-disposition", "attachment;filename="
				+ new Timestamp(System.currentTimeMillis()).getTime() + ".csv");
		response.setContentType("application/csv");
		
		TradingManage tsradingManage = serviceSession.getService(TradingManage.class);
		PagingResult<JyglEntity> tsradings = tsradingManage.tradingSearch(new TradingQuery() {

			@Override
			public EntrustType getWtzt() {
				return EntrustType.JYZ;
			}

			@Override
			public String getUsername() {
				return request.getParameter("userName");
			}
			@Override
			public JyztStatus getJyztStatus() {
				return EnumParser.parse(JyztStatus.class, request.getParameter("jylx"));
			}

			@Override
			public int getId() {
				return IntegerParser.parse(request.getParameter("id"));
			}

			@Override
			public Timestamp getCreateTimeStart() {
				return TimestampParser.parse(request.getParameter("start"));
			}

			@Override
			public Timestamp getCreateTimeEnd() {
				return TimestampParser.parse(request.getParameter("end"));
			}

			@Override
			public String getBuyer() {
				return null;
			}

			@Override
			public String getSeller() {
				return null;
			}

			@Override
			public int getXsts() {
				// TODO Auto-generated method stub
				return 0;
			}
		}, new Paging() {

			@Override
			public int getSize() {
				return  Integer.MAX_VALUE;
			}

			@Override
			public int getCurrentPage() {
				return IntegerParser.parse(request.getParameter(PAGING_CURRENT));
			}
		});
		tsradingManage.export(tsradings.getItems(), response.getOutputStream(), "");
	}
}
